<?php

function url_for($url) {
    return Router::url($url);
}

/**
 * 返回来源地址
 *
 * @return String
 */
function url_pre()
{
	return $_SERVER['HTTP_REFERER'];
}

function flashMsg($msg) {
    $_SESSION['flash_massage'] = $msg;
}

function getFlashMsg() {
    if (empty($_SESSION['flash_massage'])) {
        return false;
    }
    $msg = $_SESSION['flash_massage'];
    unset($_SESSION['flash_massage']);
    return $msg;
}

/**
 * 简化echo
 *
 * @param string $str
 */
function e($str) {
    echo $str;
}

/**
 * 驼峰字符串转换,将以_分割的字符串转换为以大写分割
 *
 * @param string $lowerCaseAndUnderscoredWord
 * @return string
 */
function camelize($lowerCaseAndUnderscoredWord) {
    $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
    return $replace;
}

/**
 * 将以大写分割的字符串转换为以_分割
 *
 * @param string $camelCasedWord
 * @return string
 */
function underscore($camelCasedWord) {
    $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
    return $replace;
}

function get_block($module, $block, $vars = array()) {
    $m = camelize($module);
    $b = camelize($block);
    
    $block_class = $m.'Block';
    $module = new $block_class;
    $params = Request::getInstance()->getParams();
    $params['module'] = $m;
    $params['action'] = $b;
    $params += $vars;
    
    $module->setParams($params);
    $module->setView(new PhpView());
    $module->getView()->layout = null;
    $module->getView()->webroot = Request::getInstance()->getParams('webroot');
    
    ob_start();
    call_user_func(array($module, $b.'Block'));
    return ob_get_clean();
}
